﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;

namespace timetable
{
    partial class CourseSchedule
    {
       
        // 对于特定位置不能上某些课程进行判断
        private bool CanCourseCellExchangeByPosition(CourseCell exchangeCell1, CourseCell exchangeCell2)
        {
            // 特定位置只能够存放特定课程
            string course1 = exchangeCell1.CourseName.Substring(0, 2);
            string course2 = exchangeCell2.CourseName.Substring(0, 2);
            // 判断交换位置后是否符合要求
            if (CanCellInThisPosition(course1,exchangeCell2.PositionClass) &&
                CanCellInThisPosition(course2, exchangeCell1.PositionClass))
            {
                return true;
            }

            return false;
        }

        // 判断课程调换到该位置是否违反了该位置的课程限制
        private bool CanCellInThisPosition(string course, int PositionClass)
        {
            return ExchangeRuleList[PositionClass].Contains(course);
        }

        public void GetCouseTimeTable(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open);
            StreamReader sr = new StreamReader(fileStream);
            string courseTime = sr.ReadLine();
            // 混合班其实没有读取的必要
            while (courseTime != "混合班")
            {
                string[] line = courseTime.Split(' ');
                string name = line[0];
                int time = int.Parse(line[1]);

                // 体音美
                if (name.Substring(0, 2) == "体育" || name.Substring(0, 2) == "音乐" || name.Substring(0, 2) == "美术")
                {
                    int groupNum = int.Parse(name.Substring(2, name.Length - 2));
                    GroupList list = resultGroupLists[groupNum];
                    foreach (Group @group in list.List)
                    {
                        // 修改每个group中的对应的课表课程
                        CourseCell cell = group.Curriculum.table[time / CourseNumADay][time % CourseNumADay];
                        cell.CourseName = name.Substring(0, 2);
                        cell.isSpecialCourse = 0;
                        cell.Class = cell.CourseName;
                    }
                }

                for (int k = 0; k < resultGroupLists.Count; k++)
                {
                    GroupList list = resultGroupLists[k];
                    // 当课程集中包含该课程的时候才深入遍历修改课表
                    if (list.CourseSet.Contains(name))
                    {
                        foreach (Group @group in list.List)
                        {
                            // 修改每个group中的对应的课表课程
                            CourseCell cell = group.Curriculum.table[time / CourseNumADay][time % CourseNumADay];
                            cell.CourseName = name.Substring(0, 2);
                            cell.isSpecialCourse = (name.Contains('_') ? 2 : 0);

                        }
                    }
                }
                courseTime = sr.ReadLine();
            }
            sr.Close();
        }
    }
}